#include "MySQLReader.h"

namespace great_config {
	MySQLReader::MySQLReader(const string &config) : Reader(config) {
		_dbConfig = Util::split(config, _dbConfig, ':');		
	}

	MySQLReader::~MySQLReader () {
		_dbConfig.clear();
	}

	
	map<string, string> &MySQLReader::getConfig(map<string, string> &map) {
		unsigned int port = atoi(_dbConfig[1].c_str());
		Connection conn(_dbConfig[2].c_str(), _dbConfig[0].c_str(), 
				_dbConfig[3].c_str(), _dbConfig[4].c_str(), port > 0 ? port : 3306);
		Query query = conn.query();
		query << "SELECT k,v,ver FROM config_" << _group;	
		StoreQueryResult result = query.store();

		map.clear();
		for(size_t i = 0, len = result.num_rows(); i < len; i ++) {
			map[result[i][0].c_str()] = result[i][1].c_str();
		}

		conn.disconnect();
		return map;
	}

}
